package com.mydemo.temp;

import org.apache.commons.lang.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Description:
 *
 * @author guoxinghong
 * @date 2021/03/12
 */

public class DateUtils {
    public static final String secondFormat = "yyyy-MM-dd HH:mm:ss";
    public static final String PATTERN_FULL = "yyyyMMddHHmmss";
    public static final String YMD = "yyyyMMdd";
    public static final String HMS = "HHmmss";
    public static final String Y_M_D = "yyyy-MM-dd";

    /**
     * 获取现在时间,指定返回时间格式
     *
     * @param pattern
     * @return
     */
    public static String getStringDate(String pattern) {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static String getStringDate(Date date, String pattern) {
        if (date == null) return null;
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        String dateString = formatter.format(date);
        return dateString;
    }

    public static String getStringDateYMD(Date date) {
        return getStringDate(date, YMD);
    }

    public static String getStringDateYMDHHMMSS(Date date) {
        return getStringDate(date, PATTERN_FULL);
    }

    public static String getStringDateYYYYMMDD(Date date) {
        return getStringDate(date, Y_M_D);
    }

    public static String getStringDateSecond(Date date) {
        return getStringDate(date, secondFormat);
    }

    public static Date parse(String dateStr, SimpleDateFormat dateFormat) {
        try {
            if (dateStr != null) {
                return dateFormat.parse(dateStr);
            }
        } catch (ParseException e) {
            throw new RuntimeException("date parse exception :" + e.getMessage());
        }
        return null;
    }

    public static Date parse(String dateStr, String dateFormatStr) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(dateFormatStr);
        return parse(dateStr, dateFormat);
    }

    public static Date parseYYYYMMDD(String dateStr) {
        return parse(dateStr, Y_M_D);
    }

    public static Date parseSecondTime(String dateStr) {
        return parse(dateStr, secondFormat);
    }

    /**
     * 取得两个日期的间隔天数
     *
     * @param one
     * @param two
     * @return 间隔天数
     */
    public static long getDiffDays(Date one, Date two) {
        Calendar sysDate = new GregorianCalendar();

        sysDate.setTime(one);

        Calendar failDate = new GregorianCalendar();

        failDate.setTime(two);
        return (sysDate.getTimeInMillis() - failDate.getTimeInMillis()) / (24 * 60 * 60 * 1000);
    }

    public static long getDiffDaysBetweenNow(Date compareDate) {
        String now = getStringDate(Y_M_D);
        Date nowDate = parseYYYYMMDD(now);
        return getDiffDays(compareDate, nowDate);
    }

    public static long getDiffDaysBetweenNow(String compareDate) {
        String now = getStringDate(Y_M_D);
        return getDiffDays(parseYYYYMMDD(compareDate), parseYYYYMMDD(now));
    }

    /**
     * 获取时间 hh24:mi:ss
     *
     * @param date
     * @return
     */
    public static String getTime(Date date) {
        String time = getStringDateSecond(date);
        if (StringUtils.isBlank(time)) {
            return time;
        }
        String[] times = time.split(" ");
        return times[1];
    }

    /**
     * 获取今天前days的时间
     *
     * @param days
     * @return
     */
    public static Date getDateBeforeNow(int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, -days);
        return calendar.getTime();
    }

    public static String getDateStrBeforeNow(int days, String format) {
        Date time = getDateBeforeNow(days);
        return getStringDate(time, format);
    }

    /**
     * 转化Java8 LocalDateTime
     *
     * @param date
     * @return
     */
    public static LocalDateTime tranlateToLocalDateTime(Date date) {
        if (date == null) {
            return null;
        }
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return instant.atZone(zoneId).toLocalDateTime();
    }

    public static boolean afterNow(String date, String format) {
        Date compareDate = parse(date, format);
        return compareDate.after(new Date());
    }

    /**
     * 时间格式校验,校验yyyy-mm-dd格式
     *
     * @param date
     * @return
     */
    public static Date validateDateFormat(String date, String prefix) {
        try {
            return DateUtils.parseYYYYMMDD(date);
        } catch (Exception e) {
            throw new IllegalArgumentException(prefix + "格式非法，请使用yyyy-mm-dd格式");
        }
    }

    public static String translateToYMD(String formatTime) {
        try {
            Date date = parse(formatTime, YMD);
            return getStringDateYYYYMMDD(date);
        } catch (Exception e) {
            throw new IllegalArgumentException("时间格式错误");
        }
    }

    public static void main(String[] args) {
        System.out.println(translateToYMD("20210311"));
    }

}
